#include "VertexArray.h"

#include "GL/glew.h"

VertexArray::VertexArray()
{
	glGenVertexArrays(1, &ID);
}

VertexArray::~VertexArray()
{
	glDeleteVertexArrays(1, &ID);
}

void VertexArray::AddBuffer(const VertexBuffer & buffer, const VertexBufferLayout & layout)
{
	Bind();

	const std::vector<VertexBufferLayoutElement>& elements =
		layout.GetElements();

	unsigned int offset = 0;
	for (int i = 0; i < elements.size(); i++) {
		const VertexBufferLayoutElement& element = elements[i];
		glEnableVertexAttribArray(i);
		glVertexAttribPointer(i, element.Count, element.Type, element.Normalize, 
			layout.GetStride(), (const void*)offset);

		offset += element.Count * VertexBufferLayoutElement::GetSizeOfType(element.Type);
	}
}

void VertexArray::Bind() const
{
	glBindVertexArray(ID);
}

void VertexArray::UnBind() const
{
	glBindVertexArray(0);
}
